<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
*   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com> 
 */

class loader {
	
	static $db;
	var $numrows;
	var $logger;
	
	public function __construct($db)
   {
   	$this->db=$db;
   	$this->logger =& LoggerManager::getLogger('loader');
   }

	public function get(&$obj) {
		$sql = $obj->get();
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();

		if ($this->numrows==0) return false;

		$row = $this->db->fetch_array();
		$obj->obj_array = $row;
		$obj->fill($row);
		$this->logger->debug("GET: ".$obj);		
		return true;
	}
	
	public function save($obj) {
		//return $this->execute($obj->insert());
		$newid = $this->db->executeInsert($obj->insert());
		#$obj->id = $newid; 
		return $newid;
	}

	public function delete($obj) {
		return $this->execute($obj->delete());
	}
	
	public function update($obj) {
		return $this->execute($obj->update());
	}
	
	public function saveOrUpdate($obj) {
		$sql = $obj->get();
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();
		
		if ($this->numrows==0)
			return $this->execute($obj->insert());
		
		return $this->execute($obj->update());
	}
	
	public function execute($sql) {
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();
		return $this->db->affected_rows();
	}

	public function executeInsert($sql) {
		$id = $this->db->executeInsert($sql);
		return $id;
	}
		
	public function loadList($obj) {
		$sql = $obj->listAll();
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();		
		$list = array();
	
		while ($row = $this->db->fetch_array()) {
			$temp = $obj->newInstance();
			$temp->obj_array = $row;
			$temp->fill($row);
			$this->logger->debug("LIST: ".$temp);
			$list[$temp->id] = $temp;
		}

		return $list;		
	}
	
	public function query($sql) {
		$this->db->query($sql);
		return $this->db->fetch_array();
	}

	public function fetchcount($sql) {
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();
		$res = $this->db->fetch_array();
		return $res[0];
	}
	
	public function count($sql) {
		$this->db->query($sql);
		return $this->db->num_rows();
	}
	
	public function fetchlist($sql,$idx = null) {
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();
		$list = array();
		
		$this->logger->debug("FETCHLIST: ".$this->numrows);
			
		while ($row = $this->db->fetch_array()) {
			$this->logger->debug($row);
			if ($idx!=null) {
				$list[$row[$idx]]=$row;
			} else {
				array_push($list,$row);	
			}
		}
		return $list;			
	}
	
	public function loadListBy($obj,$sql) {
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();
		$list = array();
			
		while ($row = $this->db->fetch_array()) {
			$temp = $obj->newInstance();
			$temp->obj_array = $row;
			$temp->fill($row);
			$this->logger->debug("LIST BY: ".$temp);
			$list[$temp->id] = $temp;
		}

		return $list;		
	}	
	
	public function getBy(&$obj,$sql) {
		$this->db->query($sql);
		$this->numrows = $this->db->num_rows();

		if ($this->numrows==0) return false;

		$row = $this->db->fetch_array();
		$obj->obj_array = $row;
		$obj->fill($row);
		$this->logger->debug("GET BY: ".$obj);	
		return true;	
	}		
}

?>
